<?php
class Model_Category extends App_Model_Entity
{
    protected $_data = array(
        'id'        => null,
	    'parent_id' => null,
	    'name'      => null,
    );

    protected  $_parent;
    protected  $_childrens;
    protected  $_products;
    protected  $_parents;
    protected  $_brands;
    protected  $_sizes;
    protected  $_categorys;

    public function __construct($data=null){
        parent::__construct($data);
        $this->_mapper = new Model_Category_Mapper();
    }

    public function __get($value)
    {
        switch($value){
            case 'parent' : return $this->getParent();
            case 'parents': {
                if($this->_parents === null){
                    $this->_parents = array();
                    $this->_parents = $this->getParents($this);
                    return $this->_parents;
                } else {
                    return $this->_parents;
                }
            }
            case 'brands':
                return $this->getBrands();
                break;
            case 'sizes':
                return $this->getSizes();
                break;
            case 'categorys':
                return $this->getCategorys();
                break;
            case 'childrens' : return $this->getChildren();
            default: return parent::__get($value);
        }
    }

    public function getChildren()
    {
        if($this->_childrens === null){
            if($this->_data['id']){
                $select = $this->_mapper->getTable()->select()->where('parent = ?', $this->_data['id']);
                $this->_childrens = $this->_mapper->getArray($this->_mapper->getTable()->fetchAll($select));
            } else {
                $select = $this->_mapper->getTable()->select()->where('parent=0');
                $this->_childrens =  $this->_mapper->getArray($this->_mapper->getTable()->fetchAll($select));
            }
        }
        return $this->_childrens;
    }

    public function getProductsByPage($page, $itemCountPerPage =30, $params = array(), $order = array())
    {

        if($this->_products === null){
            $mapper = new Model_Product_Mapper();
            $select = $mapper->getTable()->select()->where('category_id = ' . $this->_data['id']);
            foreach($params as  $key=>$value){
                if($value){
                    $select->where("$key = '$value'");
                }
            }
            if($order['price']){
                $select->order('price DESC');
            } else {
                 $select->order('price');
            }
            $paginator = new Zend_Paginator(new App_Paginator_Adapter_DbTableSelect($select, $mapper));
            $paginator->setItemCountPerPage($itemCountPerPage);
            $paginator->setCurrentPageNumber($page);
            $this->_products = $paginator;
        }
        return $this->_products;
    }

    public function getParent()
    {
        if($this->_parent === null && $this->parent_id != null){
            $select = $this->_mapper->getTable()->select()->where('id = ' . (int) $this->parent_id);
            $result = $this->_mapper->getArray($this->_mapper->getTable()->fetchAll($select));
            $this->_parent = $result[0];
        }
        return $this->_parent;
    }

    public function getParents( Model_Category $category)
    {
        array_push($this->_parents, $category->parent);
        if($category->parent){
            $this->getParents($category->parent);
        }
        return $this->_parents;
    }

    public function getBrands()
    {
        if($this->_brands === null){
            $select=  $this->_mapper->getTable()->getAdapter()->select()->from(array('p'=>'product'),array('brand'=>'brand'))
            ->where('p.category_id = ' . (int) $this->id)->distinct();
                $brands =  $this->_mapper->getTable()->getAdapter()->fetchAll($select);
            foreach($brands as $brand)
            {
                $this->_brands[] = $brand['brand'];
            }
        }
        return $this->_brands;
    }

    public function getSizes()
    {
        if($this->_sizes === null){
            $select=  $this->_mapper->getTable()->getAdapter()->select()->from(array('p'=>'product'),array('size'=>'size'))
                ->where('p.category_id = ' . (int) $this->id)->distinct();
            $sizes =  $this->_mapper->getTable()->getAdapter()->fetchAll($select);
            foreach($sizes as $size)
            {
                $this->_sizes[] = $size['size'];
            }
            sort($this->_sizes);
        }
        return $this->_sizes;
    }
    
    public function getCategorys()
    {
        if($this->_categorys === null){
            $select=  $this->_mapper->getTable()->getAdapter()->select()->from(array('p'=>'product'),array('category'=>'category'))
                ->where('p.category_id = ' . (int) $this->id)->distinct();
            $categorys =  $this->_mapper->getTable()->getAdapter()->fetchAll($select);
            foreach($categorys as $category)
            {
                $this->_categorys[] = $category['category'];
            }
            sort($this->_categorys);
        }
        return $this->_categorys;
    }
}